package leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yinlei
 * @date 2018/6/19
 * @since 1.0.0
 */
public class N001TwoSum {

    public static void main(String[] args) {
        int[] r = twoSum(new int[]{2, 7, 11, 15}, 9);
        System.out.println(Arrays.toString(r));
    }

    static public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int n = nums.length;

        for (int i = 0; i < n; i++) {
            int val = nums[i];
            if (i != 0) {
                int result = target - val;
                Integer resultIdx = map.get(result);

                if (resultIdx != null) {
                    return new int[] {resultIdx, i};
                }
            }
            map.put(val, i);
        }

        return null;
    }
}
